<?
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

if (reqVar('azione')=='conferma') {
	$msgPag = insPag();
}
echo getTestaPopUpHtml("Nuova pagina",$msgPag,array('width'=>500));
echo getHtmlForm();
echo getHtmlHidden('id');
echo getHtmlHidden('campo');
echo getHtmlHidden('azione','conferma');
echo getHtmlHidden('xsn',XSN);
?>
<table width='100%' cellspacing='1' cellpadding='2' border='0'>
   <tr>
      <td class='cmd1' align='right'>
         &nbsp;&nbsp;&nbsp;
         <a href="javascript:document.form1.submit()" class='cmd1'>conferma</a>
         &nbsp;&nbsp;&nbsp;
         <a href="javascript:self.close();" class='cmd1'>chiudi</a>
      </td>
   </tr>
</table>
<table width='100%' cellspacing='1' cellpadding='2' border='0'>
   <tr>
      <td  class='tit1' colspan='4' >Dati pagina</td>
   </tr>
   <tr>
      <td  class='lab1' width='20%'>Codice</td>
      <td  class='txt1' width='80%'>
         <?echo getHtmlTextCode('cod_pagina', 8,8);?>
      </td>
   </tr>
   <tr>
      <td  class='lab1'>Descrizione</td>
      <td  class='txt1'>
         <?echo getHtmlTextCode('descr', 50,80);?>
      </td>
   </tr>
   <tr>
      <td  class='lab1'>Note</td>
      <td  class='txt1'>
         <?echo getHtmlTextArea('nota', 80,5);?>
      </td>
   </tr>
</table>
<table width='100%' cellspacing='1' cellpadding='2' border='0'>
   <tr>
      <td class='cmd1' align='right' >
         &nbsp;&nbsp;&nbsp;
         <a href="javascript:document.frm1.submit()" class='cmd1'>conferma</a>
         &nbsp;&nbsp;&nbsp;
         <a href="javascript:self.close();" class='cmd1'>chiudi</a>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
echo getCodaPopUpHtml();

function insPag() {
   global $aSess, $objDB;
   $messErr = '';
   $_REQUEST['cod_pagina'] = strtoupper(reqVarTrim('cod_pagina'));
	if (strlen(reqVar('cod_pagina'))< 3) {
		$messErr .= "Codice pagina di almeno 3 caratteri<br>";
	}
	if (preg_match("/[^A-Z0-9]/",reqVar('cod_pagina'))) {
		$messErr = "Codice '".reqVar('cod_pagina')."' puo' contenere solo lettere numeri<br>";
	}
	$sql = "SELECT id_pagina FROM wfm_pagine
            WHERE id_modulo = ".reqVarInt('id')."
               AND cod_pagina = '".reqVar('cod_pagina')."'";
   $objDB->exec($sql);
   if ($objDB->numRec() > 0) {
		$msg .= "Codice gia' esistente<br>";
   }
   if (trim($_POST['descr']) == '') {
      $messErr .= "descrizione obbligatoria<br>";
   }
   if ($messErr != '') {
      return $messErr;
   }
   $sql = "INSERT INTO wfm_pagine SET
            cod_pagina = '".reqVar('cod_pagina')."',
            descr = '".reqVarAS('descr')."',
            nota = '".reqVarAS('nota')."',
            id_modulo = ".reqVarInt('id');
   $objDB->exec($sql,'ins');
   //header('location:listPag.php?xsn='.XSN.'&mdl='.uessp('cod_modulo'));
   echo("<script language=\"javascript\">\n");
   echo("try{\n");
   echo("function caricaPagina(campo, idPagina, codPagina){\n");
  	echo("	//alert('campo: ' + campo + '\\nidpag: ' + idPagina + '\\ndescr: ' + codPagina);\n");
  	echo("	var objCtrl = opener.document.getElementById(campo);\n");
  	echo("	if (!objCtrl) alert('Campo '+campo+' inesistente');\n");
  	echo("	var newOption = opener.document.createElement('option');\n");
  	echo("	newOption.value = idPagina;\n");
  	echo("	newOption.innerHTML = codPagina;\n");
  	echo("	objCtrl.appendChild(newOption);\n");
  	echo(" 	objCtrl.selectedIndex = objCtrl.options.length - 1;\n");
  	echo("	objCtrl = null;\n");
  	echo("	self.close();\n");
  	echo("}\n");
   echo("caricaPagina('".reqVar('campo')."',".$objDB->lastInsertId('ins').",'".reqVar('cod_pagina')."');\n");
   echo("}catch(e){\n");
   echo("	alert(e.message);\n");
   echo("}");
   echo("</script>\n");
   exit();
}
